???.computed.isNew   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 3
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 0
dl 3
loc 3
c 0
b 0
f 0
cc 1
rs 10
1 View Code Duplication
import axios from 'axios'
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
export default {
4
  props: ['id'],
5
  data() {
6
    return {
7
      validation: {},
8
      pending: false
9
    }
10
  },
11
  computed: {
12
    isNew() {
13
      return this.id === undefined
14
    }
15
  },
16
  methods: {
17
    async fetchData() {
18
      if (!this.isNew) {
19
        let { data } = await axios.get(
20
          this.$app.route(`admin.${this.resourceRoute}.show`, {
21
            [this.modelName]: this.id
22
          })
23
        )
24
25
        Object.keys(data).forEach(key => {
26
          if (key in this.model) {
27
            this.model[key] = data[key]
28
          }
29
        })
30
        this.onModelChanged()
31
      }
32
    },
33
    onModelChanged() {},
34
    feedback(name) {
35
      if (this.state(name)) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.state(name) is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
36
        return this.validation.errors[name][0]
37
      }
38
    },
39
    state(name) {
40
      return this.validation.errors !== undefined &&
41
        this.validation.errors.hasOwnProperty(name)
42
        ? 'invalid'
43
        : null
44
    },
45
    async onSubmit() {
46
      this.pending = true
47
      let router = this.$router
48
      let action = this.isNew
49
        ? this.$app.route(`admin.${this.resourceRoute}.store`)
50
        : this.$app.route(`admin.${this.resourceRoute}.update`, {
51
            [this.modelName]: this.id
52
          })
53
54
      let formData = this.$app.objectToFormData(this.model)
55
56
      if (!this.isNew) {
57
        formData.append('_method', 'PATCH')
58
      }
59
60
      try {
61
        let { data } = await axios.post(action, formData)
62
        this.pending = false
63
64
        this.$app.noty[data.status](data.message)
65
        if (this.listPath) {
66
          router.push(this.listPath)
67
        }
68
      } catch (e) {
69
        this.pending = false
70
71
        // Validation errors
72
        if (e.response.status === 422) {
73
          this.validation = e.response.data
74
          return
75
        }
76
77
        this.$app.error(e)
78
      }
79
    }
80
  },
81
  created() {
82
    this.fetchData()
83
  }
84
}
85